using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.HIV.Domains;
using InfoExpress.WinApp.Utils;
using InfoExpress.HIV.WinApp.Business;

namespace InfoExpress.HIV.WinApp.Popups
{
  public partial class FrmChonBenhNhanDieuTri : XtraForm
  {
    #region Members and Properties

    /// <summary>
    /// Ma benh nhan.
    /// </summary>
    public string MaBenhNhan
    {
      get
      {
        return txtMaThe.Text;
      }
    }
    /// <summary>
    /// Ngay dang ky.
    /// </summary>
    public DateTime NgayDangKy
    {
      get
      {
        return deNgayDangKy.DateTime;
      }
    }
    /// <summary>
    /// Benh nhan Id.
    /// </summary>
    public int BenhNhanId { get; set; }

    #endregion

    public FrmChonBenhNhanDieuTri()
    {
      InitializeComponent();
      deNgayDangKy.DateTime = DateTime.Today;
    }

    #region Event Handlers

    private void btnSelect_Click(object sender, EventArgs e)
    {
      if (dxValidationProvider.Validate())
      {
        try
        {
          DataSet dtsResult = 
            GlobalCache.DieuTriService.KiemTraTonTai(MaBenhNhan, false, true, true, null);
          DataTable dtbResult = dtsResult.Tables[0];

          if (dtbResult.Rows.Count == 0)
          {
            if (txtMaThe.Text.Substring(3, 1) != "0")
            {
              int total = 0;
              TList<BenhNhanDangKy> benhNhanList =
                GlobalCache.BenhNhanDangKyService.GetPaged(
                string.Format("MaBenhNhan='{0}' AND DeleteFlag=0", txtMaThe.Text),
                string.Empty, 0, 1, out total);

              if (total > 0)
              {
                if (benhNhanList[0].TrangThaiId == (int)BenhNhanUtils.TrangThai.Duyet)
                {
                  ChuyenOpcBuffer chuyenOpcBuffer =
                    GlobalCache.ChuyenOpcBufferService.GetByBenhNhanId(
                      BenhNhanUtils.LayBenhNhanId(txtMaThe.Text));

                  if (chuyenOpcBuffer == null)
                  {
                    BenhNhanId = benhNhanList[0].BenhNhanId;
                    DialogResult = DialogResult.OK;
                    Close();
                  }
                  else
                    UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_008);
                }
                else
                  UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_004);
              }
              else
                UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_001);
            }
            else
              UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_002);
          }
          else
          {
            UIUtils.ShowWarningMessage(String.Format(Constants.MsgWrnChonBnDieuTri_003,
              dtbResult.Rows[0]["MaDonVi"].ToString(),
              dtbResult.Rows[0]["TenDonVi"]));
          }
        }
        catch (Exception ex)
        {
          Logger.LogCritical("FrmChonBenhNhanDieuTri::btnSelect_Click", ex, "General", null);
          UIUtils.ShowCannotLoadDataMessage();
        }
      }
    }

    #endregion 
  }
}